package org.vartist.atld.menu.task.util.base64;

import org.vartist.atld.menu.activity.util.base64.Base64Activity;
import org.vartist.atld.util.base64.Base64Util;
import org.vartist.atld.util.image.ImageUtil;
import org.vartist.atld.util.log.LogUtil;

import android.graphics.Bitmap;
import android.os.AsyncTask;

public class ConvertImageToBase64Task extends AsyncTask<String, Integer, Long> {

	private Base64Activity mActivity = null;
	private Bitmap mTargetBitmap = null;
	private String mEncodedTextToBase64 = null;
	
	public ConvertImageToBase64Task(Base64Activity activity, Bitmap targetBitmap) {
		mActivity = activity;
		mTargetBitmap = targetBitmap;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		mActivity.setProgressVisibility(true);
	}
	
	@Override
	protected Long doInBackground(String... arg0) {
		if (mTargetBitmap != null) {
			byte[] byteFile = ImageUtil.bitmapToByteArray(mTargetBitmap);
			mEncodedTextToBase64 = Base64Util.encodeBytes(byteFile);
			byteFile = null;
			
			LogUtil.d("encodedTextToBase64  : " + mEncodedTextToBase64);
		}
	
		return null;
	}

	@Override
	protected void onPostExecute(Long result) {
		super.onPostExecute(result);
		mActivity.setResultForConvertBase64(mEncodedTextToBase64);
		mActivity.setProgressVisibility(false);
	}
}